//
//  NSString+Extension.m
//  CSJQQMusic
//
//  Created by ya on 12/19/16.
//  Copyright © 2016 ya. All rights reserved.
//

#import "NSString+Extension.h"
#import <objc/message.h>



@implementation NSString (Extension)

+ (NSString *)sj_contenOfFile:(NSString *)path {

    NSError *error = nil;
    NSString *lrcFileContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    if (error != nil) {
        NSLog(@"%@", error);
        return nil;
    }

    return lrcFileContent;
}

- (NSURL *)sj_url {

    NSURL *url = [NSURL URLWithString:self];

    return url;
}


- (NSString*)sj_substringWithinBoundsLeft:(NSString*)strLeft right:(NSString*)strRight
{
    NSRange rangeSub;
    NSString *strSub;

    NSRange range;
    range = [self rangeOfString:strLeft options:0];

    if (range.location == NSNotFound) {
        return nil;
    }

    rangeSub.location = range.location + range.length;

    range.location = rangeSub.location;
    range.length = [self length] - range.location;
    range = [self rangeOfString:strRight options:0 range:range];

    if (range.location == NSNotFound) {
        return nil;
    }

    rangeSub.length = range.location - rangeSub.location;
    strSub = [[self substringWithRange:rangeSub] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    return strSub;
}

- (NSString *)sj_beforeString:(NSString *)flag {
    /// flag 位置
    NSRange range = [self rangeOfString:flag];

    if (range.location == NSNotFound) {
        return nil;
    }

    /// 取 flag 之前的字符串
    return [self substringToIndex:range.location];
}

- (NSString *)sj_afterString:(NSString *)flag {
    /// flag 位置
    NSRange range = [self rangeOfString:flag];

    if (range.location == NSNotFound) {
        return nil;
    }

    /// 取 flag 之前的字符串
    return [self substringFromIndex:range.location + 1];

}

- (NSString *(^)(NSString *))link {

    __block NSString *linked = self;

    return ^NSString *(NSString *str) {
        if (self == nil) {
            NSLog(@"link的参数不可以为空!");
            return nil;
        }
        linked = [self stringByAppendingString:str];
        return linked;
    };
}

- (NSString *(^)(NSString *))suffix {
    NSString *str = self;
    if ( ![str hasSuffix:@"."] ) {
        str = [str stringByAppendingString:@"."];
    }
    return [str link];
}


#pragma mark - 将NSString转换成UTF8编码的NSString

/**
 *  在使用网络地址时, 一般要先将url进行encode成UTF8格式的编码.
 *  否则在使用时可能报告网址不存在的错误, 这时就需要进行转换.
 */
- (NSString *)S_encodeUTF8WithString:(NSString *)string {

    /**
     *  方法1
     */
    NSString *utf8Str = string;
    NSString *unicodeStr = [NSString stringWithCString:[utf8Str UTF8String] encoding:NSUnicodeStringEncoding];
    
    
    
    
    return unicodeStr;

    /**
     *  方法2
     */
    //    NSString *urlString= [NSString stringWithFormat:@"%@", string];
    //    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, (CFStringRef)urlString, NULL, NULL,  kCFStringEncodingUTF8 ));
    //    NSURL *url = [NSURL URLWithString:encodedString];
    //    NSLog(@"%@", url);
}


#pragma mark - 乱码问题
- (NSString *)S_MessyString{

    NSString *transString = [NSString stringWithString:[self stringByRemovingPercentEncoding]];

    /**
     *  以前的方法
     */
    //    NSString *transString = [NSString stringWithString:[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    return transString;
}

#pragma mark - 判断字符串 纯数字
- (BOOL)S_isPureIntWithString {

    NSScanner *scan = [NSScanner scannerWithString:self];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

#pragma mark -
#pragma mark - Base64 转码
+ (NSString *)base64EncodeString:(NSString *)string {
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

#pragma mark -
#pragma mark - Base64 解码
+ (NSString *)base64DecodeString:(NSString *)string {
    NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:0];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}


//// check(@"A").check(@"B").check(@"C").check(@"D");





@end
